﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DotNet.Data
{
    public class pageSeoDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int pageSeoId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from pageSeo where pageSeoId=@pageSeoId  ");
            htSyn.Add("pageSeoId", pageSeoId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得页面优化列表
        /// <summary>
        /// 获得页面优化列表
        /// </summary>
        /// <returns></returns>
        public IList<pageSeo> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select pageSeoId,Title,pageKey,Description,pageTop,Bottom,pageName,fromPage from pageSeo ");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 根据页面优化号查询实体
        /// <summary>
        /// 根据页面优化号查询实体
        /// </summary>
        /// <param name="pageSeoId"></param>
        /// <returns></returns>
        public pageSeo GetModel(int pageSeoId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select pageSeoId,Title,pageKey,Description,pageTop,Bottom,pageName,fromPage from pageSeo ");
            strSql.Append(" where pageSeoId=@pageSeoId ");
            htSyn.Add("@pageSeoId", pageSeoId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            pageSeo pageseo = new pageSeo();
            if (dt.Rows.Count>0)
            {
                pageseo.pageSeoId = Convert.ToInt32(dt.Rows[0]["pageSeoId"]);
                pageseo.Title = dt.Rows[0]["Title"].ToString();
                pageseo.pageKey = dt.Rows[0]["pageKey"].ToString();
                pageseo.Description = dt.Rows[0]["Description"].ToString();
                pageseo.pageTop = dt.Rows[0]["pageTop"].ToString();
                pageseo.Bottom = dt.Rows[0]["Bottom"].ToString();
                pageseo.pageName = dt.Rows[0]["pageName"].ToString();
                pageseo.fromPage = dt.Rows[0]["fromPage"].ToString();
            }           
            return pageseo;
        }
        #endregion

        #region 修改页面优化信息
        /// <summary>
        /// 修改页面优化信息
        /// </summary>
        /// <param name="pageseo"></param>
        /// <returns></returns>
        public bool Update(pageSeo pageseo)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update pageSeo set ");
            strSql.Append("Title=@Title,");
            strSql.Append("pageKey=@pageKey,");
            strSql.Append("Description=@Description,");
            strSql.Append("pageTop=@pageTop,");
            strSql.Append("Bottom=@Bottom,");
            strSql.Append("pageName=@pageName,");
            strSql.Append("fromPage=@fromPage");
            strSql.Append(" where pageSeoId=@pageSeoId ");
            htSyn.Add("@pageSeoId",pageseo.pageSeoId);
            htSyn.Add("@Title",pageseo.Title);
            htSyn.Add("@pageKey",pageseo.pageKey);
            htSyn.Add("@Description",pageseo.Description);
            htSyn.Add("@pageTop",pageseo.pageTop);
            htSyn.Add("@Bottom",pageseo.Bottom);
            htSyn.Add("@pageName",pageseo.pageName);
            htSyn.Add("@fromPage", pageseo.fromPage);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除页面优化根据页面优化号
        /// <summary>
        /// 删除页面优化根据页面优化号
        /// </summary>
        /// <param name="pageSeoId"></param>
        /// <returns></returns>
        public bool Delete(int pageSeoId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from pageSeo where pageSeoId=@pageSeoId");
            htSyn.Add("pageSeoId", pageSeoId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加页面优化
        /// <summary>
        /// 添加页面优化
        /// </summary>
        /// <param name="pageSeo"></param>
        /// <returns></returns>
        public bool Insert(pageSeo pageseo)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into pageSeo([Title],[pageKey],[Description],[pageTop],[Bottom],[pageName],[fromPage]) values (@Title,@pageKey,@Description,@pageTop,@Bottom,@pageName,@fromPage)");
            htSyn.Add("@Title", pageseo.Title);
            htSyn.Add("@pageKey", pageseo.pageKey);
            htSyn.Add("@Description", pageseo.Description);
            htSyn.Add("@pageTop", pageseo.pageTop);
            htSyn.Add("@Bottom", pageseo.Bottom);
            htSyn.Add("@pageName", pageseo.pageName);
            htSyn.Add("@fromPage", pageseo.fromPage);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<pageSeo> getProductsBySql(string strSql)
        {
            IList<pageSeo> list = new List<pageSeo>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    pageSeo pageseo = new pageSeo();
                    pageseo.pageSeoId = Convert.ToInt32(dr["pageSeoId"]);
                    pageseo.Title = dr["Title"].ToString();
                    pageseo.pageKey = dr["pageKey"].ToString();
                    pageseo.Description = dr["Description"].ToString();
                    pageseo.pageTop = dr["pageTop"].ToString();
                    pageseo.Bottom = dr["Bottom"].ToString();
                    pageseo.pageName = dr["pageName"].ToString();
                    pageseo.fromPage = dr["fromPage"].ToString();
                    list.Add(pageseo);
                }
            }
            return list;
        }
        #endregion

        #region 根据sql语句查询实体---郑同飞
        /// <summary>
        /// 根据sql语句查询实体---郑同飞
        /// </summary>
        /// <param name="pageType">优化的页面</param>
        /// <returns></returns>
        public pageSeo getSeoBySql(string pageType)
        {           
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);           
            strSql.Append("select pageSeoId,Title,pageKey,Description,pageTop,Bottom,pageName,fromPage from pageSeo where pageName=@pageType");
            htSyn.Add("@pageType", pageType);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            pageSeo pageseo = new pageSeo();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                { 
                    pageseo.pageSeoId=Convert.ToInt32(dr["pageSeoId"].ToString());                   
                    pageseo.Title = dr["Title"].ToString();
                    pageseo.pageKey = dr["pageKey"].ToString();
                    pageseo.Description = dr["Description"].ToString();
                    pageseo.pageTop = dr["pageTop"].ToString();
                    pageseo.Bottom=dr["Bottom"].ToString();
                    pageseo.pageName=dr["pageName"].ToString();
                    pageseo.fromPage=dr["fromPage"].ToString();
                }
            }
            return pageseo;

        }
         #endregion
    }
}
